package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.google.common.collect.Sets;
import com.haierp.enums.OrderStatus;
import com.haierp.exception.ErpCommonException;
import com.haierp.exception.InventoryException;
import com.haierp.mapper.ErpReturnOrderMapper;
import com.haierp.model.ErpOrder;
import com.haierp.model.ErpReturnOrder;
import com.haierp.service.IErpOrderService;
import com.haierp.service.IErpReturnOrderService;
import com.haierp.service.IShippingOrderService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.ShiroUtil;
import com.haierp.vo.ErpReturnOrderQueryVO;
import com.haierp.vo.json.PicModel;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

@Service("erpReturnOrderService")
public class ErpReturnOrderServiceImpl extends SuperServiceImpl<ErpReturnOrderMapper, ErpReturnOrder>  implements IErpReturnOrderService {
	@Autowired
	ErpReturnOrderMapper erpReturnOrderMapper;
	@Autowired
	private IErpOrderService erpOrderService;
	@Autowired
	private IShippingOrderService shippingOrderService;
	
	@Override
	public JsonPageResult<List<ErpReturnOrder>> queryErpReturnOrders(ErpReturnOrderQueryVO erpReturnOrderQueryVO) {
		JsonPageResult<List<ErpReturnOrder>> erpReturnOrderResult = new JsonPageResult<>();
		// 1、查询总的记录数量
		Integer totalCount = erpReturnOrderMapper.queryErpReturnOrdersCount(erpReturnOrderQueryVO);
		// 2、查询分页记录
		if (totalCount != null && totalCount != 0L) {
			erpReturnOrderResult.buildPage(totalCount, erpReturnOrderQueryVO);
			List<ErpReturnOrder> erpReturnOrders = erpReturnOrderMapper.queryErpReturnOrders(erpReturnOrderQueryVO);
			erpReturnOrders.forEach(erpReturnOrder -> {
				String proofImg = erpReturnOrder.getProofImg();
				if(proofImg!=null && !proofImg.equals("[]")) {
					String[] pm = HaiJsonUtils.toBean(proofImg, String[].class);
	    			erpReturnOrder.setProofImg(pm[0]);
				} else {
					erpReturnOrder.setProofImg(null);
				}
			});
			erpReturnOrderResult.setData(erpReturnOrders);
		} else {
			List<ErpReturnOrder> erpReturnOrders = new ArrayList<>();
			erpReturnOrderResult.setData(erpReturnOrders);
		}
		return erpReturnOrderResult;
	}
	
	@Override
	public void add(ErpReturnOrder erpReturnOrder) throws InventoryException {
		ErpOrder erpOrder = erpOrderService.selectById(erpReturnOrder.getErpOrderId());
		Set<Long> mainIds = Sets.newHashSet();
		if(erpOrder.getStatus()==OrderStatus.CLOSE.getCode()) {		//已关闭
			throw new ErpCommonException("已关闭的子订单不能退单");
		} else if(erpOrder.getStatus()==OrderStatus.INIT.getCode()) {	//还未发货
			//未发货的子订单，不应填写退货数量；
			if(erpReturnOrder.getReturnQuantity()!=null && erpReturnOrder.getReturnQuantity()>0) {
				throw new ErpCommonException("未发货的子订单不应填写退货数量，如果需要部分退货，请拆分子订单后进行退单操作");
			} else {
				erpOrder.setCloseReason("退单");
				erpOrderService.closeErpOrder(erpOrder);
				mainIds.add(erpOrder.getOuterOrderId());
			}
		} else if(erpOrder.getStatus()==OrderStatus.SENT.getCode() && erpReturnOrder.getReturnQuantity()!=null && erpReturnOrder.getReturnQuantity().intValue()>erpOrder.getQuantity().intValue()) {	//已发货
			throw new ErpCommonException("退货数量异常!");
		} else if(erpOrder.getStatus()==OrderStatus.SENT.getCode() && erpReturnOrder.getReturnQuantity()!=null && erpReturnOrder.getReturnQuantity().intValue()==erpOrder.getQuantity().intValue()) {	//已发货
			erpOrder.setCloseReason("退单");
			erpOrder.setStatus(OrderStatus.CLOSE.getCode());
			mainIds.add(erpOrder.getOuterOrderId());
		}
		
		ShiroUser shiroUser = ShiroUtil.getShiroUser();
		erpReturnOrder.setUserCreate(shiroUser.getLoginName());
		erpReturnOrder.setGmtCreate(new Date());
		erpReturnOrder.setGmtModify(new Date());
		this.baseMapper.insert(erpReturnOrder);
		
		erpOrder.setErpReturnOrderId(erpReturnOrder.getId());
		erpOrder.setGmtModify(new Date());
		erpOrderService.updateSelectiveById(erpOrder);
		
		//更新主订单发货状态
		if(CollectionUtils.isNotEmpty(mainIds)) {
			shippingOrderService.updateOuterstatus(mainIds);
		}
	}

	
	@Override
	public List<ErpReturnOrder> queryErpReturnOrderForExcel(ErpReturnOrderQueryVO erpReturnOrderQueryVO) {
		
		return this.baseMapper.queryErpReturnOrderForExcel(erpReturnOrderQueryVO);
	}
}
